package com.service.user.impl;

import com.dao.ChatDao;
import com.entity.ChatUser;
import com.entity.Message;
import com.service.user.ChatService;
import com.service.user.UserService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/** 提供聊天，包括聊天列表、消息等的功能实现 */
@Service
public class ChatServiceImpl implements ChatService {

  @Autowired UserService userService;

  /**
   * 返回用户编号为userId用户的未读消息总条数
   *
   * @param userId 用户编号
   * @return 未读消息总条数
   */
  @Override
  public int getUnreadCount(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ChatDao dao = session.getMapper(ChatDao.class);
    int count = dao.selectUnreadCounts(userId);
    session.close();
    return count;
  }

  /**
   * 获取用户的聊天用户列表
   *
   * @param userId 用户编号
   * @return 用户的聊天对方用户列表
   */
  @Override
  public List<ChatUser> getChatList(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ChatDao dao = session.getMapper(ChatDao.class);
    List<ChatUser> chatUsers = dao.selectChatList(userId);
    for (ChatUser chatUser : chatUsers) {
      chatUser.setUnreadCount(dao.selectUnreadCount(userId, chatUser.getId()));
    }
    session.close();
    return chatUsers;
  }

  /**
   * 返回构建与hisUserId聊天框的对方的信息
   *
   * @param myUserId 发起者的编号
   * @param hisUserId 对方的编号
   * @return 对方的信息
   */
  @Override
  public ChatUser getChatUser(String myUserId, String hisUserId) {
    ChatUser user = new ChatUser();
    user.setId(hisUserId);
    Map<String, String> nickImage = userService.getUserLink(hisUserId);
    user.setName(nickImage.get("nickname"));
    user.setImageURL(nickImage.get("imageURL"));

    SqlSession session = SqlSessionGetter.getSqlSession();
    ChatDao dao = session.getMapper(ChatDao.class);
    int unreadCount = dao.selectUnreadCount(myUserId, hisUserId);
    session.close();

    user.setUnreadCount(unreadCount);

    return user;
  }

  /**
   * 获取与指定用户的从mesgStartIndex开始的mesgNum条聊天记录 * 查看消息后，就将所有消息设置为已查看
   *
   * @param myUserId 自己的用户编号
   * @param hisUserId 对方的用户编号
   * @param mesgStartIndex 消息开始索引
   * @param mesgNum 获取消息的条数
   * @return 从指定索引开始的指定至多指定条数条聊天记录
   */
  @Override
  public List<Message> getMesgs(
      String myUserId, String hisUserId, int mesgStartIndex, int mesgNum) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ChatDao dao = session.getMapper(ChatDao.class);
    List<Message> mesgs = dao.selectMessageList(myUserId, hisUserId, mesgStartIndex, mesgNum);

    if (mesgs != null) { // 两个id都是正确的
      dao.updateMesgsStatus(myUserId, hisUserId);
      session.commit();
    }

    session.close();
    return mesgs;
  }

  /**
   * 向指定用户发送消息
   *
   * @param myUserId 本方用户编号
   * @param hisUserId 对方用户编号
   * @param content 消息内容
   * @return 发送成功返回1，否则返回0
   */
  @Override
  public int sendMessage(String myUserId, String hisUserId, String content) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ChatDao dao = session.getMapper(ChatDao.class);
    int row = 0;
    try {
      row = dao.insertMessage(myUserId, hisUserId, content);
    } catch (Exception exp) {
      // id 错误
    }
    session.commit();
    session.close();
    return row;
  }
}
